$(function(){

 //时间戳转换年月日
 function paddZero(n) { //补零函数
    return n > 10 ? n : '0' + n
  }
  
  function formatTime(time) {
    let d = new Date(time)
  
    let y = d.getFullYear()
    let month = paddZero(d.getMonth() + 1)
    let day = paddZero(d.getDate())
  
  
    return `${y}/${month}/${day}`
  }

 $.ajax({
    method:'get',
    url:' http://124.223.14.236:8060/index/list?page=1&limit=10&cateid=4',
    success:function({data}){
        console.log(data);
        console.log(data.list.data);
      let newList = data.list.data.map((item)=>{
            // console.log(item);
            let { author, catename, title, keywords, pic, tags, istop, lovenum, description, create_date, click, id} = item 

            return`
            <article class="excerpt excerpt-one">
                  <header>
                    <a class="cat label label-important" href="">享·优惠<i class="label-arrow"></i></a>
                    <h2>
                      <a target="_blank" id='indexid' href="show.html"  title="${title}" data-id="${id}"  id="show">${title}/三年279元</a>
                    </h2>
                  </header>
                  <p class="text-muted time">
                    <a href="">${author}</a> 发布于 ${formatTime(create_date)}
                  </p>
                  <p class="focus">
                    <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
                      <span  class="item"><span class="thumb-span"><img  src="http://124.223.14.236:8060/${pic}"  class="thumb" /></span></span></a>
                    </p>
                  <p class="note">${description}</p>
                  <p class="text-muted views">
                    <span class="post-views">阅读(${click})</span>
                    <span class="post-comments">评论(0)</span>
                    <a href="javascript:;" class="post-like">
                      <i class="fa fa-thumbs-up"></i>赞 (<span>${lovenum}</span>)
                    </a>
                    <span class="post-tags">标签：
                      <a href=""  rel="tag">${tags}</a> / 
                      <a href=""  rel="tag">${catename}</a></span>
                   </p>
                </article>
            `
        }).join('')
        $('.excerpt').html(newList)
    }
 })


   //   右侧广告位
   $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/index/index',
    success: function ({ data }) {
      // console.log(data);
      // console.log(data.sideBanner);

      let rightList = data.sideBanner.map((item) => {
        let { advimgdesc, advimglink, advimgsrc } = item
        return `
            <div class="textwidget">
            <a href="${advimglink}" rel="nofollow" target="_blank" title="阿里云开学季">
              <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${advimgsrc}" alt="${advimgdesc}" >
            </a>
           
          </div>
            `
      }).join('')
      $(".textwidget").html(rightList)


      //右侧 有情链接
    let newLianjie = data.allLink.map((item) => {
        // console.log(item);

        let { title, url, img, des } = item
         return `
              <li>
              <a href="${url}">
                <img src="http://124.223.14.236:8060/${img}" alt="">
                <p>${des}</p>
              </a>
            </li>
              `
      }).join('')
      $('#widget').html(newLianjie)

      // 热门标签
      let newBiaoqian = data.allTag.map((item) => {
        return `
            <li><a title="" href="">${item.tagname}</a></li>
            `
      }).join('')
      $('.widget_tags_inner').html(newBiaoqian)
    }
  })


    // 热门文章
    $.ajax({
        method: 'get',
        url: 'http://124.223.14.236:8060/home/index/recommend',
        success: function ({ data }) {
          // console.log(data);
          // console.log(data.list);
          let newrec = data.list.map((item) => {
            let { pic, title, lovenum } = item
    
            return `
                <li>
                <a target="_blank" href="">
                  <span class="thumbnail">
                    <img src="http://124.223.14.236:8060/${pic}" class="thumb" />
                  </span>
                  <span class="text">${title}</span>
                  <span class="text-muted post-views">${lovenum}</span>
                </a>
              </li>
                `
          }).join('')
          $('.widget .items-02').html(newrec)
        }
      })


})
$('article').on('click','#indexid',function(){
    const id = $(this).attr('data-id')
    localStorage.setItem('id',id)
  })